package com.strong.model;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.strong.enum_.GenderEnum;
import com.strong.utils.IdNumberUtils;
import lombok.Getter;

import java.util.Date;

import static com.strong.faker.IdNumberFaker.STR_ID_DATE_FORMAT;
import static com.strong.utils.IdNumberUtils.INTS_FEMALE_NUMBER;
import static com.strong.utils.IdNumberUtils.INTS_MALE_NUMBER;

/**
 * 身份证 模型类
 *
 * @author simen
 * @date 2023/04/07
 */
@Getter
public class IdNumber extends BaseModel {

    /**
     * 身份证号码
     */
    private final String idNumber;

    /**
     * 性别枚举对象
     */
    private final GenderEnum genderEnum;

    /**
     * 生日对象
     */
    private final Date birthday;

    /**
     * 地方对象
     */
    private final Place place;

    /**
     * 实例化
     *
     * @param place      地方对象
     * @param genderEnum 性别枚举对象
     * @param birthday   生日对象
     */
    public IdNumber(Place place, GenderEnum genderEnum, Date birthday) {
        this.place = place;
        this.genderEnum = genderEnum;
        this.birthday = birthday;

        String strBirthday = DateUtil.format(this.birthday, STR_ID_DATE_FORMAT);

        StringBuilder sbIdNumber = new StringBuilder();
        sbIdNumber.append(place.getIdPlaceCode()).append(strBirthday).append(getNumber()).append(IdNumberUtils.getCheckCode(sbIdNumber));
        this.idNumber = sbIdNumber.toString();
    }

    /**
     * 根据性别枚举 获取身份证顺序号字符串
     *
     * @return {@link String}
     */
    private String getNumber() {
        // 根据性别生成性别随机字符串
        String strGender = this.genderEnum.equals(GenderEnum.MALE) ?
                RandomUtil.randomEle(INTS_MALE_NUMBER, 5).toString() :
                RandomUtil.randomEle(INTS_FEMALE_NUMBER, 5).toString();

        // 生成随机编号
        int intNumber = RandomUtil.randomInt(1, 99);
        String strNumber = StrUtil.padPre(Integer.toString(intNumber), 2, "0");

        return strNumber + strGender;
    }

}
